package cn.t.web.order;

import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.core.mode.Result;
import cn.t.dto.order.CreateReplenishmentRequest;
import cn.t.dto.order.ReplenishmentDetailDTO;
import cn.t.dto.order.ReplenishmentExceptionDTO;
import cn.t.dto.order.ReplenishmentExceptionRequest;
import cn.t.dto.order.base.OrderSnRequest;
import cn.t.dto.order.base.VBaseDTO;
import cn.t.dto.order.repenlement.ReplenishmentDetailSkuDTO;
import cn.t.dto.order.repenlement.ReplenishmentInfoDTO;
import cn.t.dto.order.repenlement.ReplenishmentOrderRequest;
import cn.t.dto.order.repenlement.ReplenishmentPageDTO;
import cn.t.dto.order.repenlement.ReplenishmentQueryRequest;
import cn.t.facade.order.ReplenishmentDetailFacade;
import cn.t.facade.order.ReplenishmentFacade;
import cn.t.utils.ExcelUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 补货订单表服务入口
 *
 * @author l
 * @since 2024-05-24 10:15:14
 */
@Slf4j
@RestController
@RequestMapping("replenishment")
@AllArgsConstructor
public class ReplenishmentController {

    @Resource
    private ReplenishmentFacade replenishmentFacade;
    @Resource
    private ReplenishmentDetailFacade replenishmentDetailFacade;


    /**
     * 转寄售补货订单sku合并查询
     */
    // "转寄售补货订单sku合并查询", httpMethod = "POST")
    @PostMapping(value = "/toOmsOrderDetail")
    public ReplenishmentInfoDTO toOmsOrderDetail(@RequestBody ReplenishmentOrderRequest request) {
        return replenishmentFacade.replenishmentDetailSku(request.getRsSn());
    }

    /**
     * 补货单列表
     */
    @PostMapping(value = "/replenishmentList")
    public PageInfoDTO<ReplenishmentPageDTO> replenishmentList(@RequestBody PageQueryDTO<ReplenishmentQueryRequest> request) {
        return replenishmentFacade.queryList(request);
    }

    /**
     * 创建补货单
     *
     * @return
     */
    @PostMapping(value = "/createReplenishment")
    public Result<String> createReplenishment(@Validated @RequestBody CreateReplenishmentRequest request) {
      return     Result.success(replenishmentFacade.createReplenishment(request));
    }

    /**
     * 关闭补货单
     */
    @PostMapping(value = "/closeReplenishment")
    public void closeReplenishment(@RequestBody ReplenishmentQueryRequest request) {
        if (ObjectUtils.isEmpty(request.getRsSn())) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, "rsSn不能为空");
        }
           replenishmentFacade.closeReplenishment(request.getRsSn());

    }

    /**
     * 补货单详情
     */
    @PostMapping(value = "/replenishmentDetail")
    public VBaseDTO<ReplenishmentPageDTO,ReplenishmentDetailDTO> replenishmentDetail(@Validated @RequestBody OrderSnRequest request) {
        String rsSn = request.getOrderSn();
        if (ObjectUtils.isEmpty(rsSn)) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, "rsSn不能为空");
        }
        ReplenishmentPageDTO replenishmentInfo = replenishmentFacade.findReplenishmentInfo(rsSn);
        List<ReplenishmentDetailDTO> replenishmentDetailList = replenishmentDetailFacade.getReplenishmentDetailList(Collections.singletonList(rsSn));
     return    new VBaseDTO<>(replenishmentInfo, replenishmentDetailList);
    }

    /**
     * 补货单详情-sku维度
     */
    @PostMapping(value = "/replenishmentDetailSkuDimension")
    public VBaseDTO<ReplenishmentPageDTO,ReplenishmentDetailSkuDTO> replenishmentDetailSkuDimension( @Validated @RequestBody OrderSnRequest request) {
        String rsSn = request.getOrderSn();

        if (ObjectUtils.isEmpty(rsSn)) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, "rsSn不能为空");
        }
        ReplenishmentPageDTO replenishmentInfo = replenishmentFacade.findReplenishmentInfo(rsSn);
        List<ReplenishmentDetailSkuDTO> replenishmentDetailSkuDTOS = replenishmentDetailFacade.replenishmentDetailSkus(rsSn);
        return new VBaseDTO<>(replenishmentInfo, replenishmentDetailSkuDTOS);
    }

    /**
     * 导出补货单详情
     */
    @PostMapping(value = "/export/exportReplenishmentDetail")
    public void exportReplenishmentDetail(@Validated @RequestBody OrderSnRequest request, HttpServletResponse response) {
        String rsSn = request.getOrderSn();
        if (ObjectUtils.isEmpty(rsSn)) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, "rsSn不能为空");
        }
        List<ReplenishmentDetailDTO> replenishmentDetailList = replenishmentDetailFacade.getReplenishmentDetailList(Collections.singletonList(rsSn));
        ReplenishmentPageDTO replenishmentInfo = replenishmentFacade.findReplenishmentInfo(rsSn);
        try {
            ExcelUtil.exportExcel(response, replenishmentDetailList, replenishmentInfo, "exportrxcel/RsDetail.xlsx", "补货单详情导出");
        } catch (Exception e) {
          log.error("导出补货单详情异常", e);
        }
    }

    /**
     * 补货单详情-sku维度-导出
     *
     */
    @PostMapping(value = "/export/replenishmentDetailSkuDimension")
    public void exportReplenishmentDetailSkuDimension(@Validated @RequestBody OrderSnRequest request, HttpServletResponse response) {
        String rsSn = request.getOrderSn();
        if (ObjectUtils.isEmpty(rsSn)) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, "rsSn不能为空");
        }
        List<ReplenishmentDetailSkuDTO> replenishmentDetailSkuDTOS = replenishmentDetailFacade.replenishmentDetailSkus(rsSn);
        ReplenishmentPageDTO replenishmentInfo = replenishmentFacade.findReplenishmentInfo(rsSn);
        try {
            ExcelUtil.exportExcel(response, replenishmentDetailSkuDTOS, replenishmentInfo, "exportrxcel/RsSkuDetail.xlsx", "补货单详情导出");
        } catch (Exception e) {
            log.error("导出补货单详情异常", e);
        }
    }

    /**
     * 异常补货单列表
     */
    @PostMapping(value = "/replenishmentErrList")
    public PageInfoDTO<ReplenishmentExceptionDTO> replenishmentExceptionList(@Validated @RequestBody PageQueryDTO<ReplenishmentExceptionRequest> request) {
            return replenishmentFacade.getErrRsList(request);
    }
}

